package org.yray.sip.creator;



import org.yray.sip.base.MsgEle;
import org.yray.sip.server.SipSession;

import java.util.Optional;


public class CancelCreator extends MessageCreator {
    /***
     *
     CANCEL sip:3308913430565773@172.168.200.78:5060 SIP/2.0
     Via: SIP/2.0/UDP 172.130.13.31:57326;rport;branch=z9hG4bKPj8933f6c16ccf4f1b9c5cf6524982ac5c
     Max-Forwards: 70
     From: "78002" <sip:78002@172.168.200.78>;tag=34b692c57eae49258e1ab213d39aed20
     To: <sip:3308913430565773@172.168.200.78>
     Call-ID: 6c74a2ae050a42fd9b3daf78d0d0ab32
     CSeq: 4315 CANCEL
     Route: <sip:172.168.200.78:5060;lr>
     User-Agent: MicroSIP/3.20.3
     Content-Length:  0
     * @param session
     * @return
     */
    @Override
    public String createMsg(SipSession session){

        StringBuilder sb = new StringBuilder();
        sb.append("CANCEL sip:"+session.getTo()+"@"+session.getSipIpPort()+";transport=udp SIP/2.0").append(MsgEle.LINE_SEPARATOR);
        sb.append("Via: SIP/2.0/UDP "+ session.getLocalIpPort()+";rport;branch="+session.getBranch()).append(MsgEle.LINE_SEPARATOR);
        sb.append("Max-Forwards: 70").append(MsgEle.LINE_SEPARATOR);
        Optional.ofNullable(session.getSipMap()).ifPresent(
                map->{
                    Optional.ofNullable(session.getSipMap().get(MsgEle.CALL_ID)).ifPresent(v->{
                        sb.append(MsgEle.CALL_ID+": ").append(v).append(MsgEle.LINE_SEPARATOR);
                    });
                    Optional.ofNullable(session.getSipMap().get(MsgEle.FROM)).ifPresent(v->{
                        sb.append(MsgEle.FROM+": ").append(v).append(MsgEle.LINE_SEPARATOR);
                    });
                    Optional.ofNullable(session.getSipMap().get(MsgEle.TO)).ifPresent(v->{
                        sb.append(MsgEle.TO+": ").append(v).append(MsgEle.LINE_SEPARATOR);
                    });
                }
        );
        sb.append("CSeq: "+session.getCseq()+" CANCEL").append(MsgEle.LINE_SEPARATOR);
        sb.append("Content-Length:  0").append(MsgEle.LINE_SEPARATOR);
        sb.append(MsgEle.LINE_SEPARATOR);
        return sb.toString();
    }


}
